package com.zyyt.springboot.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zyyt.springboot.bean.ApiResult;
import com.zyyt.springboot.bean.Student;
import com.zyyt.springboot.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    private String prefix = "student";

    @GetMapping()
    public String notice()
    {
        return prefix + "/list";
    }
    /**
     * 学生信息列表
     * @param student
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(int page, int limit,Student student){
        page = Math.max(page, 1);
        limit = limit < 1 ? 10: limit;
        Page<Student> studentpage = PageHelper.startPage(page, limit);
        studentService.queryStudentList(student);
        //重新封装返回参数,用于页面显示start
        Map<String, Object> result = new HashMap<>();
        result.put("data",studentpage.getResult());
        result.put("code",0);
        result.put("count",studentpage.getTotal());
        result.put("msg","学生信息列表");
        //重新封装返回参数,用于页面显示end
        return result;
    }
    /**
     * 修改公学生信息
     */
    @GetMapping("/edit")
    public String edit(Long id, ModelMap mmap) {
        Student student = studentService.queryStudentById(id);
        mmap.put("student", student);
        return prefix + "/edit";
    }
    /**
     * 学生信息查看
     * @param id 主键ID
     * @return
     */
    @RequestMapping("/queryStudentById")
    @ResponseBody
    public Object queryStudentById(long id) {
        Student student = studentService.queryStudentById(id);
        return student!=null ? ApiResult.success(student) : ApiResult.fail("学生信息不存在");
    }
    /**
     * 学生信息新增
     * @param student
     * @return
     */
    @RequestMapping("/addStudent")
    @ResponseBody
    public Object addStudent(@Validated Student student) {
        int cnt =  studentService.addStudent(student);
        return cnt>0 ? ApiResult.success("新增成功") : ApiResult.fail("新增失败");
    }
    /**
     * 学生信息更新
     * @param student
     * @return
     */
    @RequestMapping("/updateStudent")
    @ResponseBody
    public Object updateStudent(@Validated Student student) {
        int cnt =  studentService.updateStudentById(student);
        return cnt>0 ? ApiResult.success("修改成功") : ApiResult.fail("修改失败");
    }
    /**
     * 学生信息删除
     * @param id 主键ID
     * @return
     */
    @RequestMapping("/deleteStudentById")
    @ResponseBody
    public Object deleteStudentById(long id) {
        int cnt  =  studentService.deleteStudentById(id);
        return cnt>0 ? ApiResult.success("删除成功") : ApiResult.fail("删除失败");
    }
}
